package in.srain.cube.views.ptr;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;

import in.srain.cube.views.ptr.indicator.PtrIndicator;

/**
 * Created by wenjiezeng on 2017/11/9.
 */
public class PtrPurongHeader extends FrameLayout implements PtrUIHandler  {
    private ImageView mImageView;
    private AnimationDrawable progressDra;

    public PtrPurongHeader(Context context) {
        super(context);
        initView();
    }

    public PtrPurongHeader(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public PtrPurongHeader(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void initView(){
        View header = LayoutInflater.from(getContext()).inflate(R.layout.purong_head, this);
        mImageView=(ImageView) header.findViewById(R.id.image_view);
        progressDra= (AnimationDrawable) mImageView.getDrawable();
    }

    @Override
    public void onUIReset(PtrFrameLayout frame) {
//        Log.e("zwj","...onUIReset");
        findViewById(R.id.main_lay).setVisibility(GONE);
        progressDra.stop();

    }

    @Override
    public void onUIRefreshPrepare(PtrFrameLayout frame) {
        findViewById(R.id.main_lay).setVisibility(VISIBLE);
        mImageView.setImageResource(R.drawable.pullrefresh_animation);
        progressDra= (AnimationDrawable) mImageView.getDrawable();
//        Log.e("zwj","...onUIRefreshPrepare");
        progressDra.stop();
    }

    @Override
    public void onUIRefreshBegin(PtrFrameLayout frame) {
//        Log.e("zwj","...onUIRefreshBegin");
        progressDra.start();
    }

    @Override
    public void onUIRefreshComplete(PtrFrameLayout frame) {
        Log.e("zwj","...onUIRefreshComplete");
        progressDra.stop();

    }

    @Override
    public void onUIPositionChange(PtrFrameLayout frame, boolean isUnderTouch, byte status, PtrIndicator ptrIndicator) {
//        Log.e("zwj","...onUIPositionChange");
        // 获取总的头部可下拉的距离：
//        final int offsetToRefresh = frame.getOffsetToRefresh();
        // 获取当前手指已经下拉的距离：
//        final int currentPos = ptrIndicator.getCurrentPosY();
//        if (currentPos<=0){
//            findViewById(R.id.main_lay).setVisibility(GONE);
//        }
//        Log.e("zwj","...offsetToRefresh="+offsetToRefresh+",currentPos="+currentPos);
    }
}
